from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from app.schemas.ai_character import AICharacter, AICharacterCreate, AICharacterUpdate
from app.services.ai_service import create_ai_character, get_ai_character, get_user_ai_characters, update_ai_character, delete_ai_character

router = APIRouter()

@router.post("/", response_model=AICharacter)
async def create_character(character: AICharacterCreate):
    """创建AI角色"""
    try:
        new_character = await create_ai_character(character)
        return new_character
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )

@router.get("/{character_id}", response_model=AICharacter)
async def read_character(character_id: str):
    """获取AI角色详情"""
    character = await get_ai_character(character_id)
    if not character:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="AI character not found"
        )
    return character

@router.get("/", response_model=List[AICharacter])
async def read_characters(skip: int = 0, limit: int = 100):
    """获取AI角色列表"""
    characters = await get_user_ai_characters(skip=skip, limit=limit)
    return characters

@router.put("/{character_id}", response_model=AICharacter)
async def update_character(character_id: str, character: AICharacterUpdate):
    """更新AI角色"""
    updated_character = await update_ai_character(character_id, character)
    if not updated_character:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="AI character not found"
        )
    return updated_character

@router.delete("/{character_id}")
async def delete_character(character_id: str):
    """删除AI角色"""
    result = await delete_ai_character(character_id)
    if not result:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="AI character not found"
        )
    return {"message": "AI character deleted successfully"}